package com.fast.linkbeanadmin.security.impl;

import com.fast.linkbeanadmin.enums.CodeEnum;
import com.fast.linkbeanadmin.pojo.response.ResultBean;
import com.fast.linkbeanadmin.utils.response.ResponseUtil;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruan cai yuan
 * @version 1.0
 * @fileName com.fast.linkbeanadmin.security.handlerImpl.AuthenticationFailureHandlerImpl
 * @description: 登录失败的拦截器
 * @since 2024/7/21 下午5:51
 */
@Component
public class AuthenticationFailureHandlerImpl implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        // 抛出异常?  filter
        ResultBean result = ResultBean.error(CodeEnum.AUTHENTICATION_FAILED.getCode(), CodeEnum.AUTHENTICATION_FAILED.getMsg());
        ResponseUtil.responseData(response, result, MediaType.APPLICATION_JSON_VALUE);
    }
}
